###########################################################
#
# Makefile to generate revision info header as needd
#
###########################################################
SVN_REV := $(subst Last Changed Rev: ,,$(shell svn info | grep "Last Changed Rev:"))
ifeq ($(strip $(SVN_REV)),)
SVN_REV := -1
endif

SVN_MODIFIED := $(shell svn status --quiet | grep "^[MCAD]"|wc -l)
ifeq ($(strip $(SVN_REV)),)
SVN_MODIFIED := -1
endif

SVN_URL:= $(subst URL: ,,$(shell svn info | grep URL:))
ifeq ($(strip $(SVN_URL)),)
SVN_URL :=UNKNOWN
SVN_SHORT_URL:= UNKNOWN
else
SVN_SHORT_URL:= $(subst https://gumstix-dfrank.googlecode.com/svn/,,$(SVN_URL))
endif

SVN_DATE:= $(subst Last Changed Date: ,,$(shell svn info | grep Date:))
ifeq ($(strip $(SVN_DATE)),)
SVN_DATE :=UNKNOWN
endif

revision_info: $(INCDIR)/revision_info.h

revision_clean: 
	rm -f $(INCDIR)/revision_info.h

$(INCDIR)/revision_info.h: $(DEVROOT)/build_env/revision_info.mk
	@echo "/**" > $@
	@echo " *  File: $<" >> $@
	@echo " *  AUTO GENERATED FILE - DO NOT EDIT" >> $@
	@echo " */ " >> $@
	@echo "" >> $@
	@echo "#ifndef __VERSION_INFO_H__" >> $@
	@echo "#define __VERSION_INFO_H__" >> $@
	@echo "" >> $@
	@echo "#define SVN_MODIFIED "$(SVN_MODIFIED)"" >> $@
	@echo "#define SVN_REV $(SVN_REV)" >> $@
	@echo "#if SVN_MODIFIED != 0" >> $@
	@echo "#define SVN_URL \"$(SVN_URL)\"" >> $@
	@echo "#define SVN_SHORT_URL \"$(SVN_SHORT_URL)\"" >> $@
	@echo "#else" >> $@
	@echo "#define SVN_URL \"$(SVN_URL) (MODIFIED)\"" >> $@
	@echo "#define SVN_SHORT_URL \"$(SVN_SHORT_URL) (MODIFIED)\"" >> $@
	@echo "#endif" >> $@
	@echo "#define SVN_DATE \"$(SVN_DATE)\"" >> $@
	@echo "" >> $@
	@echo "#define DECLARE_SVN_REV_FUNCTION(prefix) extern \"C\" unsigned long prefix##svnInfo(const char** url, const char** date);" >>$@
	@echo "#define CREATE_SVN_REV_FUNCTION(prefix) extern \"C\" \\" >>$@
	@echo "{ \\" >>$@
	@echo "unsigned long prefix##_svnInfo(const char** url, const char** date) \\" >>$@
	@echo "{ \\" >>$@
	@echo "    static const char* kURL = SVN_URL;\\" >>$@
	@echo "    static const char* kDATE = SVN_DATE;\\" >>$@
	@echo "    if(url) *url = kURL;\\" >>$@
	@echo "    if(date) *date = kDATE;\\" >>$@
	@echo "    return SVN_REV;\\" >>$@
	@echo "} /*prefix##_svnInfo*/ \\" >>$@
	@echo "} /* extern \"C\"*/" >>$@
	@echo "" >> $@
	@echo "#define CALL_SVN_REV_FUNCTION(prefix, url,date) prefix##_svnInfo((url),(date))" >>$@
	@echo "" >> $@
	@echo "#endif /* __VERSION_INFO_H__ */" >> $@
